package com.zyd.xtgl.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zyd.common.AjaxUtils;
import com.zyd.common.CommonController;
import com.zyd.xtgl.domain.vo.TbAdmDept;
import com.zyd.xtgl.domain.vo.TbAdmUser;
import com.zyd.xtgl.logic.IAdmDeptFacade;
import com.zyd.xtgl.logic.IAdmUserFacade;

@Controller
@RequestMapping("/AdmDept")
public class AdmDeptAction extends CommonController {
	private IAdmUserFacade admUserImpl;
	private IAdmDeptFacade admDeptImpl;

	@Resource(name = "admDeptImpl")
	public void setAdmDeptImpl(IAdmDeptFacade admDeptImpl) {
		this.admDeptImpl = admDeptImpl;
	}

	@Resource(name = "admUserImpl")
	public void setAdmUserImpl(IAdmUserFacade admUserImpl) {
		this.admUserImpl = admUserImpl;
	}

	/**
	 * 转换为json数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getList")
	public String getList(HttpServletRequest request,
			HttpServletResponse response, @RequestParam int page,
			@RequestParam int rows) {
		List<TbAdmDept> deptlist = admDeptImpl.getDeptPageList((page - 1)
				* rows, rows);
		long total = admDeptImpl.getDeptPageCount();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", deptlist);
		return AjaxUtils.ajaxList(request, response, map);
	}

	/**
	 * 获取所有公司信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getDeptList")
	public String getDeptList(HttpServletRequest request,
			HttpServletResponse response) {
		List<TbAdmDept> list = admDeptImpl.getAllDeptList();
		return AjaxUtils.ajaxList(request, response, list);
	}

	/**
	 * 公司管理列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getDeptMain")
	public ModelAndView getDeptMain(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("xtgl/deptlist");
	}

	/**
	 * 添加公司
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addDept")
	public ModelAndView addDept(HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("deptform") @Valid TbAdmDept deptBean,
			BindingResult result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		if (result.hasErrors()) {
			out.write("输入有误,添加公司失败~~~~");
		} else if (admDeptImpl.checkDeptName(deptBean)) {
			out.write("添加失败，公司名称重复~~");
		} else {
			admDeptImpl.addDept(deptBean);
			out.write("添加成功~~");
		}
		out.close();
		return null;
	}

	/**
	 * 删除公司
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(params = "method=deleteDept")
	public ModelAndView deleteDept(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		String deptid = request.getParameter("deptid");
		PrintWriter out = response.getWriter();
		List<TbAdmUser> list = admUserImpl.getUserListOfDept(deptid);
		if (list.size() > 0) {
			out.write("部门正在被使用，不能删除~~");
		} else {
			admDeptImpl.delDept(deptid);
			out.write("删除成功~~");
		}
		out.close();
		return null;
	}

	/**
	 * 更新公司信息
	 * 
	 * @param request
	 * @param response
	 * @param deptBean
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateDept")
	public ModelAndView updateDept(HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("deptform") @Valid TbAdmDept deptBean,
			BindingResult result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		admDeptImpl.updateDept(deptBean);
		out.write("更新成功~~");
		out.close();
		return null;
	}
}
